#include <U8g2lib.h>

#define INT_PIN 5
#define READ_PIN 16
#define RESET_PIN 0

U8G2_SSD1306_128X64_NONAME_F_4W_HW_SPI u8g2(U8G2_R0, /* cs=*/2, /* dc=*/0, /* reset=*/4);

volatile unsigned long time_now = 0;
volatile unsigned long last_time = 0;

//volatile int state = LOW;
//int code[95] = { 1,0,1,0,0,1,0,0,1,1,0,1,1,1,1,0,1,0,0,1,1,1,0,1,0,1,1,0,0,0,1,0,0,0,0,1,0,1,0,1,1,1,0,0,1,0,1,0,1,0,1,0,1,0,0,0,0,1,0,1,1,1,0,0,1,0,0,1,1,1,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,1,0,0,1,0,0,0,1,0,1 };
int code[95] = { 0 };
volatile int index_my = 0;
volatile bool fresh_flag = true;
volatile bool read_ok_flag = false;
void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(READ_PIN, INPUT_PULLUP);
  pinMode(RESET_PIN, INPUT_PULLUP);
  Serial.begin(9600);
  u8g2.begin();
  //u8g2.setContrast(0);
  u8g2.clearBuffer();
  u8g2.setFont(u8g2_font_ncenB08_tr);
  //attachInterrupt(0, read_data, CHANGE);
  attachInterrupt(digitalPinToInterrupt(INT_PIN), read_data, CHANGE);
}

void loop() {

  //复位
  if (!digitalRead(RESET_PIN)) {
    //attachInterrupt(0, read_data, CHANGE);
    attachInterrupt(digitalPinToInterrupt(INT_PIN), read_data, CHANGE);
    index_my = 0;
    memset(code, 0, 95 * 2);
    fresh_flag = true;
    read_ok_flag = false;
  }

  //绘制条形码
  if (fresh_flag) {
    u8g2.clearBuffer();
    u8g2.setDrawColor(1);
    u8g2.drawBox(0, 0, 128, 64);

    for (int i = 0; i < 95; i++) {
      if (code[i] == 1) {
        u8g2.setDrawColor(0);  //黑
        if (i == 0 || i == 2 || i == 46 || i == 48 || i == 92 || i == 94) {
          u8g2.drawLine(i + 10, 0, i + 10, 50);
        } else {
          u8g2.drawLine(i + 10, 0, i + 10, 45);
        }
      }
    }

    //显示已读数据个数
    char str[3];
    sprintf(str, "%2d", index_my);
    u8g2.setDrawColor(0);  //黑
    u8g2.drawStr(116, 58, str);

    //显示解析数据
    if (read_ok_flag) {
      int data[13] = { 0 };
      char output[95] = { 0 };
      char data_char[13] = { 0 };

      //转换类型
      int_to_char(code, output, 95);

      //解码
      if (decoding(output, data)) {
        Serial.println("now to jie ma");
        //转换类型
        int_to_char(data, data_char, 13);

        char data1[7] = { 0 };
        data1[6] = '\0';

        u8g2.setDrawColor(0);  //黑
        //头
        memcpy(data1, data_char, 1);
        data1[1] = '\0';
        u8g2.drawStr(3, 56, data1);

        //左边数据
        memcpy(data1, data_char + 1, 6);
        u8g2.drawStr(17, 56, data1);

        //右边数据
        memcpy(data1, data_char + 7, 6);
        u8g2.drawStr(63, 56, data1);
      } else {
        u8g2.drawStr(116, 10, "E");
      }
      read_ok_flag = false;
    }
    u8g2.sendBuffer();
    fresh_flag = false;
  }
}


//将int数组转成char数组
void int_to_char(int* pint, char* pchar, int num) {
  for (int i = 0; i < num; i++) {
    pchar[i] = pint[i] + '0';
    Serial.print(pchar[i]);
  }
  Serial.println();
}
ICACHE_RAM_ATTR void read_data() {
  time_now = millis();
  if (time_now - last_time > 10) {
    int data = digitalRead(READ_PIN);
    digitalWrite(LED_BUILTIN, data);
    code[index_my] = data;
    index_my++;

    //如果读完
    if (index_my == 95) {
      index_my == 0;
      detachInterrupt(digitalPinToInterrupt(INT_PIN));
      read_ok_flag = true;
    }

    fresh_flag = true;
    last_time = millis();
  }
}